<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Apache域名301跳转 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Apache域名301跳转</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个站点难免会有多个域名，而多个域名总得有一个主次，比如一个网站有两个域名访问，但不管用哪个域名访问，最终都会跳到其中固定的一个域名上去。这个行为就叫域名跳转， 301 只是一个状态码，跳转除了301 还有 302.如何配置跳转</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># vim /usr/local/apache2/conf/extra/httpd-vhosts.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在对应的虚拟主机配置文件中加入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;IfModule mod_rewrite.c&gt;</div><div class="line">    RewriteEngine on</div><div class="line">    RewriteCond %&#123;HTTP_HOST&#125; ^www.aaa.com$</div><div class="line">    RewriteRule ^/(.*)$ http://www.test.com/<span class="variable">$1</span> [R=301,L]</div><div class="line">&lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存退出。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># apachectl -t</span></div><div class="line">Syntax OK</div><div class="line">[root@lamp ~]<span class="comment"># apachectl graceful</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检测配置是否错误，并重启 apache 服务。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果是多个域名，这样设置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;IfModule mod_rewrite.c&gt;</div><div class="line">    RewriteEngine on</div><div class="line">    RewriteCond %&#123;HTTP_HOST&#125; ^www.aaa.com [OR]</div><div class="line">    RewriteCond %&#123;HTTP_HOST&#125; ^www.bbb.com$</div><div class="line">    RewriteRule ^/(.*)$ http://www.test.com/<span class="variable">$1</span> [R=301,L]</div><div class="line">&lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;IfModule mod_rewrite.c&gt;</div><div class="line">    RewriteEngine on</div><div class="line">    RewriteCond %&#123;HTTP_HOST&#125; !^www.test.com$</div><div class="line">    RewriteRule ^/(.*)$ http://www.test.com/<span class="variable">$1</span> [R=301,L]</div><div class="line">&lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启 apache 后，在浏览器访问 www.aaa.com 会直接跳转到 www.test.com 。也可以直接在 linux 命令下使用 curl 命令。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># curl -x127.0.0.1:80 www.aaa.com -I</span></div><div class="line">HTTP/1.1 301 Moved Permanently</div><div class="line">Date: Thu, 05 Jan 2017 03:36:42 GMT</div><div class="line">Server: Apache/2.2.31 (Unix) PHP/5.6.6</div><div class="line">Location: http://www.test.com/</div><div class="line">Content-Type: text/html; charset=iso-8859-1</div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Apache/25. Apache域名301跳转/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
